<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\banner\admin;

use app\banner\model\Banner as banner;
use think\Db;
use app\common\builder\ZBuilder;
use app\admin\controller\Admin;

class Index extends Admin
{
    function _initialize()
    {
        parent::_initialize();
        $_SESSION['sys_projectid'] = 66;
        $this->bannerModel = new banner();
    }

    /**
     * @return mixed轮播插件后台入口
     */
    function index()
    {
        //项目标识
        // 获取查询条件
        $where = $this->getMap();
        $where['projectid'] = $_SESSION['sys_projectid'];
        $where['isdel']=2;
        $field = 'id,title,url,status,type,order,createtime';
        $banners = $this->bannerModel->where($where)->order('number_orders asc')->paginate();
        // 分页数据
        $page = $banners->render();
        return ZBuilder::make('table')
            ->setPageTitle('轮播管理')// 设置页面标题
            ->setTableName('banner_banner')// 设置数据表名
            ->setSearch(['id' => 'ID', 'title' => '轮播标题'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['__INDEX__', '序号'],
                ['id', 'ID'],
                ['title', '轮播标题'],
                ['url', '图片', 'picture'],
                ['type', '类型', 'status', '', [1 => '网页', 2 => '手机']],
                ['createtime', '创建时间', ''],
                ['status', '状态', 'status', '', ['显示', '不显示']],
                ['number_orders', '排序', '分类排序'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,delete')
            ->addRightButtons('edit,delete')// 批量添加右侧按钮
            ->setRowList($banners)// 设置表格数据
            ->fetch(); // 渲染页面
    }

    function add()
    {

        return ZBuilder::make('form')
            ->setPageTitle('添加商品')// 设置页面标题
            ->setUrl(url('addPost'))
            ->addFormItems([ // 批量添加表单项
                ['text', 'title', '轮播标题', '必填，轮播标题'],
                ['image', 'url', '轮播图'],
                ['radio', 'type', '图片类型', '', [1 => '网页', 2 => '手机'], 1],
                ['radio', 'status', '状态', '', [0 => '显示', 1 => '不显示'], 0],
                ['Number', 'number_orders', '轮播排序'],
            ])
            ->fetch();
    }

    /**
     * 添加轮播图
     * @param title 标题
     * @param url   图片地址
     * @param status 状态 0显示 1不显示
     * @param type 类型 1网页 2手机
     * @param order 排序
     */
    function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['projectid'] = $_SESSION['sys_projectid'];
            $data['createtime'] = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
            $result = $this->bannerModel->adminAddBanner($data);
            if ($result) {
                $this->success('添加成功!', url('index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    /**
     * 编辑轮播
     * @param $id  轮播id
     */
    function edit($id = null)
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('非法操作!', url('index'));
        }
        $where['id'] = intval($id);
        //项目标识
        $where['projectid'] = $_SESSION['sys_projectid'];
        $field = 'id,title,url,status,type,number_orders,createtime';
        $banner = $this->bannerModel->getOneByWhere('banner_banner', $where, $field);
        return ZBuilder::make('form')
            ->setPageTitle('编辑商品')// 设置页面标题
            ->setUrl(url('editPost'))
            ->addFormItems([ // 批量添加表单项
                ['text', 'id', '商品id', '','','','','hidden'],
                ['text', 'title', '轮播标题', '必填，轮播标题'],
                ['image', 'url', '轮播图'],
                ['radio', 'type', '图片类型', '', [1 => '网页', 2 => '手机'], 1],
                ['radio', 'status', '状态', '', [0 => '显示', 1 => '不显示'], 0],
                ['Number', 'number_orders', '轮播排序'],
            ])
            ->setFormData($banner)
            ->fetch();
    }

    /**
     * 编辑轮播提交
     */


    function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //项目id
            $where['projectid'] = $_SESSION['sys_projectid'];
            $where['id'] = $data['id'];
            $result = $this->bannerModel->saveData('banner_banner', $where, $data);
            if ($result) {
                $this->success('修改成功!', url('index'));
            } else {
                $this->error('修改失败');
            }

        }
    }

    /**
     * 轮播图删除
     */

    function delete($record = [])
    {
        $param = $this->request->param();
        $ids = $param['ids'];
        $where['id'] = $ids;
        if (is_array($ids)) {
            $where['id'] = array('in', $ids);
        }
        $where['projectid'] = $_SESSION['sys_projectid'];
        $data['isdel'] = 1;
        if ($this->bannerModel->update($data, $where)) {
            //action_log('user_add', 'admin_user', $user['id'], UID);
            $this->success('修改成功', url('index'));
        } else {
            $this->error('修改失败');
        }
    }
}
